import json

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View
from apps.areas.models import Area, Address


class AreasView(View):
    def get(self, request):
        province_list = []
        city = Area.objects.filter(parent=None)
        for a in city:
            province_list.append({"id": a.id, "name": a.name})
        return JsonResponse({"code": 0, "province_list": province_list})


class AreaIdView(View):
    def get(self, requset, area_id):
        subs = []
        print(area_id, 'area_id')
        subares = Area.objects.filter(parent_id=area_id)
        print(subares)
        for a in subares:
            subs.append({"id": a.id, "name": a.name})
        return JsonResponse({"code": 0, "sub_data": {"subs": subs}})


class AddressView(View):
    def post(self, request):
        body = request.body
        data_dict = json.loads(body)
        receiver = data_dict.get('receiver')
        province_id = data_dict.get('province_id')
        city_id = data_dict.get('city_id')
        district_id = data_dict.get('district_id')
        place = data_dict.get('place')
        mobile = data_dict.get('mobile')
        tel = data_dict.get('tel')
        email = data_dict.get('email')
        # - 2 校 验
        if not all([receiver, province_id, city_id, district_id, place, mobile]):
            return JsonResponse({'code': 400, 'errmsg': "数据不全"})
        # - 3 使用Address模型类 添加数据
        try:
            address = Address.objects.create(user=request.user,
                                             title=receiver,
                                             receiver=receiver,
                                             province_id=province_id,
                                             city_id=city_id,
                                             district_id=district_id,
                                             place=place,
                                             mobile=mobile,
                                             tel=tel,
                                             email=email)
            print('address_id = ', address.id)
            request.user.default_address = address.id
            request.user.save()
        except Exception as e:
            print(e)
            return JsonResponse({'code': 505, 'errmsg': "保存失败"})
        address_dict = {
            "id": address.id,
            "title": address.title,
            "receiver": address.receiver,
            "province": address.province.name,
            "city": address.city.name,
            "district": address.district.name,
            "place": address.place,
            "mobile": address.mobile,
            "tel": address.tel,
            "email": address.email
        }
        # - 4 返回成功信息
        return JsonResponse({'code': 0, 'errmsg': "ok", 'address': address_dict})

    def get(self, request):
        # - 1 获取用户对象
        user = request.user
        # - 2获取所有地址
        # addresses = Address.objects.all(user=user)
        try:
            addresses = user.addresses.all().filter(is_deleted=False)
            print(addresses)

            # 创建一个空列表 存地址的字典数据
            address_dict_list = []
            # 循环拼接字典 添加到列表里
            for address in addresses:
                address_dict = {
                    "id": address.id,
                    "title": address.title,
                    "receiver": address.receiver,
                    "province": address.province.name,
                    "city": address.city.name,
                    "district": address.district.name,
                    "place": address.place,
                    "mobile": address.mobile,
                    "tel": address.tel,
                    "email": address.email
                }
                # - 3拼接列表
                address_dict_list.insert(0, address_dict)
        except Exception as e:
            print(e)
            return JsonResponse({'code': 501, 'errmsg': '查询失败'})

        # - 4 返回
        return JsonResponse({'code': 0, 'errmsg': 'ok', 'addresses': address_dict_list,
                             'default_address_id': request.user.default_address})


class adressdefault(View):
    def put(self, request, id):
        request.user.default_address = id
        request.user.save()
        return JsonResponse({"code": 0, "errmsg": "ok"})


class addressDelete(View):
    def delete(self, request, addresses_id):
        address = Address.objects.get(id=addresses_id)
        # 将地址逻辑删除设置为True
        address.is_deleted = True
        address.save()
        return JsonResponse({"code": 0, "errmsg": "ok"})
